package com.zhiyou.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zhiyou.entity.User;
import com.zhiyou.service.UserManagerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

@Controller
@RequestMapping("/userM")
public class UserManager {

    @Autowired
    UserManagerService userManagerService;

    @RequestMapping("/getAll")
    public String getAll(@RequestParam(value = "pn",defaultValue = "1") Integer pn ,
                         @RequestParam(value = "field",required = false) String field,
                         @RequestParam(value = "keyword",required = false)String keyword,
                         Model model){
        PageHelper.startPage(pn,20);
        List<User> all = userManagerService.getAll(field,keyword);
        PageInfo<User> userManager = new PageInfo<>(all);
        model.addAttribute("userManager",userManager);
        return "User/list";
    }

    @RequestMapping("/add")
    public String add(User user){
        userManagerService.add(user);
        return "redirect:/userM/getAll";
    }

    @RequestMapping("/delete")
    public String delete(Integer uId){
        System.out.println(uId);
        userManagerService.delete(uId);
        return "redirect:/userM/getAll";
    }

    @RequestMapping("/update")
    public String delete(User user){
        userManagerService.update(user);
        return "redirect:/userM/getAll";
    }
}
